package com.pwc.ackAdvance;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author fantuancyh
 * @data 2021/8/22
 */
@Configuration
public class ackAdvanceConfig {
    private static final String EXCHANGE_NAME = "confirm.exchange";
    private static final String QUEUE_NAME = "confirm.queue";
    private static final String ROUTING_KEY = "key1";
    //backup
    private static final String BACKUP_EXCHANGE_NAME = "backup.exchange";
    private static final String BACKUP_QUEUE_NAME = "backup.queue";
    private static final String WARNING_QUEUE_NAME = "warning.queue";

    @Bean("confirmExchange")
    public DirectExchange confirmExchange(){
        return ExchangeBuilder.directExchange(EXCHANGE_NAME)
                .durable(true)
                .withArgument("alternate-exchange",BACKUP_EXCHANGE_NAME).build();
        //return new DirectExchange(EXCHANGE_NAME,true,false);
    }
    @Bean("fanoutExchange")
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange(BACKUP_EXCHANGE_NAME,true,false);
    }

    @Bean("confirmQueue")
    public Queue confirmQueue(){
        return QueueBuilder.durable(QUEUE_NAME).build();
    }

    @Bean("backupQueue")
    public Queue backupQueue(){
        return QueueBuilder.durable(BACKUP_QUEUE_NAME).build();
    }
    @Bean("warningQueue")
    public Queue warningQueue(){
        return QueueBuilder.durable(WARNING_QUEUE_NAME).build();
    }

    @Bean
    public Binding queueBindWithExchange(
            @Qualifier("confirmQueue") Queue queue,
            @Qualifier("confirmExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }
    @Bean
    public Binding backupQueueBindWithExchange(
            @Qualifier("backupQueue") Queue backupQueue,
            @Qualifier("fanoutExchange") FanoutExchange exchange){
        return  BindingBuilder.bind(backupQueue).to(exchange);
    }
    @Bean
    public Binding warningQueueBindWithExchange(
            @Qualifier("warningQueue") Queue warningQueue,
            @Qualifier("fanoutExchange") FanoutExchange exchange){
        return  BindingBuilder.bind(warningQueue).to(exchange);
    }


}
